package me.huanmeng.heater.sender.manager;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.google.common.collect.Lists;
import me.huanmeng.heater.Heater;
import me.huanmeng.heater.exception.ResponseException;
import me.huanmeng.heater.http.KaiheilaApi;
import me.huanmeng.heater.main.HeaterMain;
import me.huanmeng.heater.object.ChannelInfo;
import me.huanmeng.heater.object.Guild;
import me.huanmeng.heater.object.UserInfo;
import me.huanmeng.heater.sender.Channel;
import org.jetbrains.annotations.NotNull;

import javax.annotation.Nullable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 2021/10/4<br>
 * Heater<br>
 *
 * @author huanmeng_qwq
 */
public class ChannelManager {
    private final Map<String, ChannelInfo> channelInfos;
    private final Map<String, Channel> channels;

    public ChannelManager() {
        this.channelInfos = new HashMap<>();
        this.channels = new HashMap<>();
        init();
    }
    void init() {
        for (Guild guild : Heater.getGuilds()) {
            for (ChannelInfo userInfo : getChannelsHttp(guild, 1,1)) {
                updateChannelInfo(userInfo);
            }
            for (ChannelInfo userInfo : getChannelsHttp(guild, 1,2)) {
                updateChannelInfo(userInfo);
            }
        }
    }

    List<ChannelInfo> getChannelsHttp(Guild guild, int page,int type) {
        List<ChannelInfo> userInfolist = Lists.newArrayList();
        final KaiheilaApi kaiheilaApi = HeaterMain.getInstance().getKaiheilaApi();
        final String body = kaiheilaApi.doGet("/channel/list")
                .form("guild_id", guild.getId())
                .form("page", page)
                .form("page_size", 30)
                .form("type",type)
                .executeAsync().body();
        final JSONObject jsonObject = new JSONObject(body);
        if (jsonObject.getInt("code", -1) != 0) {
            throw new ResponseException(body);
        }
        for (Object o : jsonObject.getJSONObject("data").getJSONArray("items")) {
            userInfolist.add(JSONUtil.toBean((JSONObject) o, ChannelInfo.class));
        }
        if (jsonObject.getJSONObject("data").getJSONObject("meta").getInt("page_total", page) > page) {
            userInfolist.addAll(getChannelsHttp(guild, page + 1,type));
        }
        return userInfolist;
    }
    @Nullable
    public Channel getChannel(String id){
        return channels.get(id);
    }
    @Nullable
    public ChannelInfo getChannelInfo(String id) {
        return channelInfos.get(id);
    }

    public void updateChannelInfo(@NotNull ChannelInfo channel) {
        if (channelInfos.containsKey(channel.getId())) {
            final ChannelInfo channelInfo = channelInfos.get(channel.getId());
            channelInfo.setId(channel.getId());
            channelInfo.setGuild_id(channel.getGuild_id());
            channelInfo.setMaster_id(channel.getMaster_id());
            channelInfo.setParent_id(channel.getParent_id());
            channelInfo.setName(channel.getName());
            channelInfo.setTopic(channel.getTopic());
            channelInfo.setType(channel.getType());
            channelInfo.setLevel(channel.getLevel());
            channelInfo.setSlow_mode(channel.getSlow_mode());
            channelInfo.setLimit_amount(channel.getLimit_amount());
            channelInfo.set_category(channel.is_category());
            channelInfo.setServer_url(channel.getServer_url());
        } else {
            channelInfos.put(channel.getId(), channel);
            channels.put(channel.getId(),new Channel(channel));
        }
    }
}
